package edu.nf.project.services.storeOperation.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import edu.nf.project.entitys.dto.storeOperation.CargoDTO;
import edu.nf.project.entitys.entity.storeOperation.convention.cargo.Cargo;
import edu.nf.project.entitys.entity.storeOperation.convention.cargo.CargoOrder;
import edu.nf.project.entitys.entity.storeOperation.convention.cargo.CargoType;
import edu.nf.project.mappers.storeOperation.convention.CargoMapper;
import edu.nf.project.mappers.storeOperation.convention.CargoOrderMapper;
import edu.nf.project.mappers.storeOperation.convention.CargoTypeMapper;
import edu.nf.project.services.storeOperation.CargoOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 货物订单Service实现类
 */
@Service
public class CargoOrderServiceImpl implements CargoOrderService {
    @Autowired
    private CargoOrderMapper cargoOrderMapper;

    @Autowired
    private CargoTypeMapper cargoTypeMapper;

    @Autowired
    private CargoMapper cargoMapper;

    @Override
    public List<CargoOrder> getAllCargoOrder() {
        return cargoOrderMapper.selectList(null);
    }

    @Override
    public CargoDTO getCargoOrder(Integer oId) {
        // 1. 获取货物订单基本信息
        CargoOrder cargoOrder = cargoOrderMapper.selectById(oId);
        if (cargoOrder == null) {
            return null;
        }

        // 2. 获取货物信息
        Cargo cargo = cargoMapper.selectById(cargoOrder.getCargoId());
        if (cargo == null) {
            CargoDTO dto = new CargoDTO();
            dto.setCargoOrder(cargoOrder);
            return dto;
        }

        // 3. 获取货物类型信息
        LambdaQueryWrapper<CargoType> typeWrapper = new LambdaQueryWrapper<>();
        typeWrapper.eq(CargoType::getCtId, cargo.getCtId());
        List<CargoType> cargoTypes = cargoTypeMapper.selectList(typeWrapper);

        // 4. 组装DTO对象
        CargoDTO cargoDTO = new CargoDTO();
        cargoDTO.setCargoOrder(cargoOrder);
        cargoDTO.setCargoType(cargoTypes);
        cargoDTO.setCargo(List.of(cargo));

        return cargoDTO;
    }

    @Override
    public List<Cargo> getAllCargo() {
        return cargoMapper.selectList(null);
    }

    @Override
    public Boolean increaseStock(Integer cargoId, Integer quantity) {
        if (cargoId == null || quantity == null || quantity <= 0) {
            return false;
        }
        com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper<Cargo> updateWrapper = new com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper<>();
        updateWrapper.eq(Cargo::getCargoId, cargoId)
                .setSql("stock_quantity = stock_quantity + " + quantity);
        return cargoMapper.update(null, updateWrapper) > 0;
    }

    @Override
    public Boolean decreaseStock(Integer cargoId, Integer quantity) {
        if (cargoId == null || quantity == null || quantity <= 0) {
            return false;
        }
        Cargo current = cargoMapper.selectById(cargoId);
        if (current == null || current.getStockQuantity() == null || current.getStockQuantity() < quantity) {
            return false;
        }
        com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper<Cargo> updateWrapper = new com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper<>();
        updateWrapper.eq(Cargo::getCargoId, cargoId)
                .setSql("stock_quantity = stock_quantity - " + quantity);
        return cargoMapper.update(null, updateWrapper) > 0;
    }

}
